package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.CarConstans;
import com.ruoyi.system.domain.SysAlarms;
import com.ruoyi.system.domain.SysCarResults;
import com.ruoyi.system.service.ISysAlarmsService;
import com.ruoyi.system.service.ISysCarResultsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysRepairInfoMapper;
import com.ruoyi.system.domain.SysRepairInfo;
import com.ruoyi.system.service.ISysRepairInfoService;

/**
 * 维修信息表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-11
 */
@Service
public class SysRepairInfoServiceImpl implements ISysRepairInfoService {
    @Autowired
    private SysRepairInfoMapper sysRepairInfoMapper;
    @Autowired
    private ISysCarResultsService sysCarResultsService;
    @Autowired
    private ISysAlarmsService sysAlarmsService;

    /**
     * 查询维修信息表
     *
     * @param repairId 维修信息表主键
     * @return 维修信息表
     */
    @Override
    public SysRepairInfo selectSysRepairInfoByRepairId(Long repairId) {
        return sysRepairInfoMapper.selectSysRepairInfoByRepairId(repairId);
    }

    /**
     * 查询维修信息表列表
     *
     * @param sysRepairInfo 维修信息表
     * @return 维修信息表
     */
    @Override
    public List<SysRepairInfo> selectSysRepairInfoList(SysRepairInfo sysRepairInfo) {
        return sysRepairInfoMapper.selectSysRepairInfoList(sysRepairInfo);
    }

    /**
     * 新增维修信息表
     *
     * @param sysRepairInfo 维修信息表
     * @return 结果
     */
    @Override
    public int insertSysRepairInfo(SysRepairInfo sysRepairInfo, Long alarmId) {
        if (sysRepairInfo == null) return -1;
        Long resId = sysRepairInfo.getResId();
        SysCarResults sysCarResults = sysCarResultsService.selectSysCarResultsByResId(resId);
        if (sysCarResults == null) return -1;
        sysRepairInfo.setStateInfoId(sysCarResults.getStateInfoId());
        int rows = sysRepairInfoMapper.insertSysRepairInfo(sysRepairInfo);

        //更新报警信息状态
        SysAlarms sysAlarms = sysAlarmsService.selectSysAlarmsByAlarmId(alarmId);
        sysAlarms.setState(CarConstans.STATE_ASSIGNED);
        rows += sysAlarmsService.updateSysAlarms(sysAlarms);
        return rows;
    }

    /**
     * 修改维修信息表
     *
     * @param sysRepairInfo 维修信息表
     * @return 结果
     */
    @Override
    public int updateSysRepairInfo(SysRepairInfo sysRepairInfo) {
        return sysRepairInfoMapper.updateSysRepairInfo(sysRepairInfo);
    }

    /**
     * 批量删除维修信息表
     *
     * @param repairIds 需要删除的维修信息表主键
     * @return 结果
     */
    @Override
    public int deleteSysRepairInfoByRepairIds(Long[] repairIds) {
        return sysRepairInfoMapper.deleteSysRepairInfoByRepairIds(repairIds);
    }

    /**
     * 删除维修信息表信息
     *
     * @param repairId 维修信息表主键
     * @return 结果
     */
    @Override
    public int deleteSysRepairInfoByRepairId(Long repairId) {
        return sysRepairInfoMapper.deleteSysRepairInfoByRepairId(repairId);
    }
}
